﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace custom
{
    internal class StartAndStop
    {
        private Process process = new Process();
        private TabControl tabControl;
        private TabPage tabPage;
        private TextBox textBox;
        private BoolWrapper isCommandExecuted = new BoolWrapper { Value = false };
        private string[] value;
        public StartAndStop(TabControl tabControl, TabPage tabPage,TextBox textBox, string[] value)
        {
            this.tabControl = tabControl;
            this.tabPage = tabPage;
            this.textBox = textBox;
            this.value = value;
        }
        public class BoolWrapper
        {
            public bool Value { get; set; }
        }
        public async void Button_Click_Start(object sender, EventArgs e)
        {
            // 切换到tabControl1控件
            tabControl.Select();
            // 切换到textBox1控件
            tabControl.SelectedTab = tabPage;
            textBox.Focus();
            //判断是否第一次执行
            if (!isCommandExecuted.Value)
            {
                //清除之前的服务停止的消息
                textBox.Clear();
                // 将标志变量设置为true
                isCommandExecuted.Value = true;
                // 设置进程属性
                process.StartInfo.FileName = "cmd.exe";
                process.StartInfo.WorkingDirectory = @value[1];
                process.StartInfo.Arguments = value[2];
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.CreateNoWindow = true;
                // 注册事件，获取cmd输出
                process.OutputDataReceived += new DataReceivedEventHandler((s, args) =>
                {
                    if (!String.IsNullOrEmpty(args.Data))
                    {
                        // 在文本框中输出cmd输出
                        textBox.Invoke(new Action(() =>
                        {
                            textBox.AppendText(args.Data + Environment.NewLine);
                        }));
                    }
                });

                // 启动进程
                process.Start();
                process.BeginOutputReadLine();

                // 等待进程结束
                await Task.Run(() => process.WaitForExit());
            }





        }
        public void Button_Click_Stop(object sender, EventArgs e)
        {
            textBox.Clear();
            // 切换到tabControl1控件
            tabControl.Select();
            // 切换到textBox1控件
            tabControl.SelectedTab = tabPage;
            textBox.Focus();
            isCommandExecuted.Value = false;
            GenerateConsoleCtrlEvent(ConsoleCtrlEvent.CTRL_C, 0);
            textBox.AppendText(value[0] + "服务已停止");
            
        }

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool GenerateConsoleCtrlEvent(ConsoleCtrlEvent sigevent, int dwProcessGroupId);

        private enum ConsoleCtrlEvent : uint
        {
            CTRL_C = 0,
            CTRL_BREAK = 1,
            CTRL_CLOSE = 2,
            CTRL_LOGOFF = 5,
            CTRL_SHUTDOWN = 6
        }
    }
}
